home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Unix / skey / src / skey.c < prev    next >
C/C++ Source or Header  |  1993-11-08  |  3KB  |  153 lines

  1. /*
  2.  * S/KEY v1.1b (skey.c)
  3.  *
  4.  * Authors:
  5.  *          Neil M. Haller <nmh@thumper.bellcore.com>
  6.  *          Philip R. Karn <karn@chicago.qualcomm.com>
  7.  *          John S. Walden <jsw@thumper.bellcore.com>
  8.  *          Scott Chasin <chasin@crimelab.com>
  9.  *
  10.  *
  11.  * Stand-alone program for computing responses to S/Key challenges.
  12.  * Takes the iteration count and seed as command line args, prompts
  13.  * for the user's key, and produces both word and hex format responses.
  14.  *
  15.  * Usage example:
  16.  *    >skey 88 ka9q2
  17.  *    Enter password:
  18.  *    OMEN US HORN OMIT BACK AHOY
  19.  *    >
  20.  */
  21.  
  22. #include <stdio.h>
  23. #ifdef HAS_STD_LIB
  24. #include <stdlib.h>
  25. #else
  26. #include <sys/types.h>
  27. #endif
  28. #include <string.h>
  29. #ifdef    __MSDOS__
  30. #include <dos.h>
  31. #else                /* Assume BSD unix */
  32. #include <fcntl.h>
  33. #include <sgtty.h>
  34. #endif
  35. #include "md4.h"
  36. #include "skey.h"
  37.  
  38. char *readpass ();
  39. void usage ();
  40. int getopt ();
  41. extern int optind;
  42. extern char *optarg;
  43.  
  44. main (argc, argv)
  45.   int argc;
  46.   char *argv[];
  47. {
  48.   int n, cnt, i, pass = 0;
  49.   char passwd[256], key[8], buf[33], *seed, *slash;
  50.  
  51.   cnt = 1;
  52.  
  53.   while ((i = getopt (argc, argv, "n:p:")) != EOF)
  54.   {
  55.     switch (i)
  56.     {
  57.     case 'n':
  58.       cnt = atoi (optarg);
  59.       break;
  60.     case 'p':
  61.       strcpy (passwd, optarg);
  62.       pass = 1;
  63.       break;
  64.     }
  65.   }
  66.  
  67.   /* could be in the form <number>/<seed> */
  68.  
  69.   if (argc <= optind + 1)
  70.   {
  71.     /* look for / in it */
  72.     if (argc <= optind)
  73.     {
  74.       usage (argv[0]);
  75.       exit (1); 
  76.     }
  77.  
  78.     slash = strchr (argv[optind], '/');
  79.     if (slash == NULL)
  80.     {
  81.       usage (argv[0]);
  82.       exit (1);
  83.     }
  84.     *slash++ = '\0';
  85.     seed = slash;
  86.  
  87.     if ((n = atoi (argv[optind])) < 0)
  88.     {
  89.       printf ("%s not positive\n", argv[optind]);
  90.       usage (argv[0]);
  91.       exit (1);
  92.     }
  93.   }
  94.   else
  95.   {
  96.  
  97.     if ((n = atoi (argv[optind])) < 0)
  98.     {
  99.       printf ("%s not positive\n", argv[optind]);
  100.       usage (argv[0]);
  101.       exit (1);
  102.     }
  103.     seed = argv[++optind];
  104.   }
  105.  
  106.   /* Get user's secret password */
  107.   if (!pass)
  108.   {
  109.     printf ("Enter secret password: ");
  110.     readpass (passwd, sizeof (passwd));
  111.   }
  112.  
  113.   rip (passwd);
  114.  
  115.   /* Crunch seed and password into starting key */
  116.   if (keycrunch (key, seed, passwd) != 0)
  117.   {
  118.     fprintf (stderr, "%s: key crunch failed\n", argv[0]);
  119.     exit (1);
  120.   }
  121.   if (cnt == 1)
  122.   {
  123.     while (n-- != 0)
  124.       f (key);
  125.     printf ("%s\n", btoe (buf, key));
  126. #ifdef    HEXIN
  127.     printf ("%s\n", put8 (buf, key));
  128. #endif
  129.    }
  130.   else
  131.   {
  132.     for (i = 0; i <= n - cnt; i++)
  133.       f (key);
  134.     for (; i <= n; i++)
  135.     {
  136. #ifdef    HEXIN
  137.       printf ("%d: %-29s  %s\n", i, btoe (buf, key), put8 (buf, key));
  138. #else
  139.       printf ("%d: %-29s\n", i, btoe (buf, key));
  140. #endif
  141.       f (key);
  142.     }
  143.   }
  144.   exit (0);
  145. }
  146.  
  147. void
  148.  usage (s)
  149.   char *s;
  150. {
  151.   printf ("Usage: %s [-n count] [-p password ] <sequence #>[/] <key> \n", s);
  152. }
  153.